package com.example.shoppingmallclient.adapter;

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shoppingmallclient.R;
import com.example.shoppingmallclient.ServerIp;
import com.example.shoppingmallclient.vo.CartItemVo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 袁梦达 2019012364
 */
public class CartListAdapter extends BaseAdapter {

    private List<CartItemVo> productList = new ArrayList<>();
    private int layoutResId;
    private Context context;
    private ImageView ivPic;
    private TextView tvBaseInformation;
    private TextView tvPrice;
    private TextView tvQuantity;
    private TextView tvSumPrice;
    private LinearLayout cartItem;

    private Integer productId;

    public CartListAdapter(List<CartItemVo> productList, int layoutResId, Context context) {
        this.productList = productList;
        this.layoutResId = layoutResId;
        this.context = context;
    }

    @Override
    public int getCount() {
        if(productList != null){
            return productList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(productList != null){
            return productList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            //加载item布局文件
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(layoutResId, null, false);
        }

        //在布局文件中显示数据

        //获取控件
        ivPic = convertView.findViewById(R.id.iv_pic);
        tvBaseInformation = convertView.findViewById(R.id.tv_base_information);
        tvPrice = convertView.findViewById(R.id.tv_price);
        tvQuantity = convertView.findViewById(R.id.tv_quantity);
        tvSumPrice = convertView.findViewById(R.id.tv_sum_price);
        cartItem = convertView.findViewById(R.id.cart_item);

        //设置控件显示内容
        ivPic.setImageBitmap(productList.get(position).getImage());
        tvBaseInformation.setText(productList.get(position).getBaseInformation());
        tvPrice.setText("单价：" + productList.get(position).getPrice());
        tvQuantity.setText("数量" + productList.get(position).getQuantity());
        tvSumPrice.setText("总价：" + productList.get(position).getSumPrice());

//        cartItem.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                new removeProductFromCart().execute(productList.get(position).getProductId());
//                productList.remove(productList.get(position));
//                return true;
//            }
//        });

        return convertView;
    }
//    private class removeProductFromCart extends AsyncTask<Integer, Object, String> {
//
//        @Override
//        protected String doInBackground(Integer... productId) {
//            String result = null;
//            try {
//                String serverUrl = "http://" + ServerIp.ServerIp + ":8080/CartServlet";
//                URL url = new URL(serverUrl);
//                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//                connection.setRequestMethod("POST");
//                OutputStream os = connection.getOutputStream();
//
//                String msg = "remove," + productId[0];
//                os.write(msg.getBytes());
//                os.flush(); //刷新缓存
//
//                //接收服务器发来的消息
//                InputStream is = connection.getInputStream();
//                StringBuffer stringBuffer = new StringBuffer();
//                int len = 0;
//                byte[] bytes = new byte[1024];
//                while ((len = is.read(bytes)) != -1) {
//                    stringBuffer.append(new String(bytes, 0, len));
//                }
//                result = stringBuffer.toString();
//                System.out.println(result);
//            } catch (ProtocolException e) {
//                e.printStackTrace();
//            } catch (MalformedURLException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            return result;
//        }
//
//        @Override
//        protected void onPostExecute(String result) {
//            if("true".equals(result)){
//                Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
//            }else {
//                Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
//            }
//            notifyDataSetChanged();
//        }
//    }
}
